// 表格列
import tagRender from '@/components/aggridCellRenDerer/tagRender.vue'
import emitter from "@/hooks/useEventBus";
import { Message } from '@arco-design/web-vue';
import { post, get } from '@/api/http'
import { slicing } from '@/utils/caseFrimAndSecRig'
import { localeText } from '@/api/aggridZw'
import { Search } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
let CompanyId: any;
let tableColumns: any = [
    {
        headerCheckboxSelection: true,
        checkboxSelection: true,
        width: 60,
        showDisabledCheckboxes: false,
        pinned: 'left',
        headerName: ''
    },
    {
        headerName: '调拨单号',
        field: 'shiftCode',
        width: 180,
    },
    {
        headerName: '调出公司',
        field: 'shiftCompanyName',
        width: 180,
    },
    // {
    //     headerName: '调出部门',
    //     field: 'departmentName',
    //     width: 180,
    // },
    // {
    //     headerName: '机台名称',
    //     field: 'materialName',
    //     width: 180,
    // },
    // {
    //     headerName: '机台区域',
    //     field: 'district',
    //     width: 180,
    // },
    // {
    //     headerName: '机台编号',
    //     field: 'numberId',
    //     width: 180,
    // },
    // {
    //     headerName: '入厂时间',
    //     field: 'joinFactoryTime',
    //     width: 180,
    // },
    // {
    //     headerName: '折旧时长',
    //     field: 'scrapTime',
    //     width: 180,
    // },
    // {
    //     headerName: '已折旧时间',
    //     field: 'deprecitionTime',
    //     width: 180,
    // },
    {
        headerName: '调入公司',
        field: 'inCompanyName',
        width: 180,
    },
    // {
    //     headerName: '调入部门',
    //     field: 'shiftDepartmentName',
    //     width: 180,
    // },
    {
        headerName: '调拨时间',
        field: 'shiftTime',
        width: 180,
    },
    {
        headerName: '调拨人',
        field: 'shiftPerson',
        width: 180,
    },
    // {
    //     headerName: '转移后差异分摊',
    //     field: 'shiftShare',
    //     width: 180,
    // },
    {
        headerName: '建立人',
        field: 'createName',
        width: 180,
    },
    {
        headerName: '建立时间',
        field: 'createTime',
        width: 180,
    },
    {
        headerName: '修改人',
        field: 'updateName',
        width: 180,
    },
    {
        headerName: '修改时间',
        field: 'updateTime',
        width: 180,
    },
    {
        headerName: '备注',
        field: 'remark',
        width: 180,
    },
    {
        headerName: '审核状态',
        field: 'auditState',
        width: 180,
        cellRenderer: tagRender,
        cellRendererParams: {
            styleClass: (params: any) => {
                return params.auditState == 0
                    ? 'warning'
                    : params.auditState == 1
                        ? 'success'
                        : params.auditState == 2
                            ? 'info'
                            : ''
            },
            text: (params: any) => {
                return params.auditState == 0
                    ? '待审核'
                    : params.auditState == 1
                        ? '审核通过'
                        : params.auditState == 2
                            ? '审核驳回'
                            : ''
            },
            eltagSize: 0,
        },
        filterParams: {
            valueFormatter: function (params: any) {
                return params.value == '0'
                    ? '待审核'
                    : params.value == '1'
                        ? '审核通过'
                        : params.value == '2'
                            ? '审核驳回'
                            : ''
            },
        }
    },
]
let tableColumns1: any = [
    {
        headerCheckboxSelection: true,
        checkboxSelection: true,
        width: 60,
        showDisabledCheckboxes: false,
        pinned: 'left',
        headerName: ''
    },
    {
        headerName: '调拨单号',
        field: 'shiftCode',
        width: 180,
    },
    {
        headerName: '调拨公司',
        field: 'shiftCompanyName',
        width: 180,
    },
    // {
    //     headerName: '调拨部门',
    //     field: 'departmentName',
    //     width: 180,
    // },
    // {
    //     headerName: '机台名称',
    //     field: 'materialName',
    //     width: 180,
    // },
    // {
    //     headerName: '机台区域',
    //     field: 'district',
    //     width: 180,
    // },
    // {
    //     headerName: '机台编号',
    //     field: 'numberId',
    //     width: 180,
    // },
    // {
    //     headerName: '入厂时间',
    //     field: 'joinFactoryTime',
    //     width: 180,
    // },
    // {
    //     headerName: '折旧时长',
    //     field: 'scrapTime',
    //     width: 180,
    // },
    // {
    //     headerName: '已折旧时间',
    //     field: 'deprecitionTime',
    //     width: 180,
    // },
    {
        headerName: '调入公司',
        field: 'inCompanyName',
        width: 180,
    },
    // {
    //     headerName: '调入部门',
    //     field: 'shiftDepartmentName',
    //     width: 180,
    // },
    {
        headerName: '调拨时间',
        field: 'shiftTime',
        width: 180,
    },
    {
        headerName: '调拨人',
        field: 'shiftPerson',
        width: 180,
    },
    // {
    //     headerName: '转移后差异分摊',
    //     field: 'shiftShare',
    //     width: 180,
    // },
    {
        headerName: '建立人',
        field: 'createName',
        width: 180,
    },
    {
        headerName: '建立时间',
        field: 'createTime',
        width: 180,
    },
    {
        headerName: '修改人',
        field: 'updateName',
        width: 180,
    },
    {
        headerName: '修改时间',
        field: 'updateTime',
        width: 180,
    },
    {
        headerName: '备注',
        field: 'remark',
        width: 180,
    },
    {
        headerName: '审核状态',
        field: 'auditState',
        width: 180,
        cellRenderer: tagRender,
        cellRendererParams: {
            styleClass: (params: any) => {
                return params.auditState == 0
                    ? 'warning'
                    : params.auditState == 1
                        ? 'success'
                        : params.auditState == 2
                            ? 'info'
                            : ''
            },
            text: (params: any) => {
                return params.auditState == 0
                    ? '待审核'
                    : params.auditState == 1
                        ? '审核通过'
                        : params.auditState == 2
                            ? '审核驳回'
                            : ''
            },
            eltagSize: 0,
        },
        filterParams: {
            valueFormatter: function (params: any) {
                return params.value == '0'
                    ? '待审核'
                    : params.value == '1'
                        ? '审核通过'
                        : params.value == '2'
                            ? '审核驳回'
                            : ''
            },
        }
    },
]
let tableColumns2: any = [
    {
        headerCheckboxSelection: true,
        checkboxSelection: true,
        width: 60,
        showDisabledCheckboxes: true,
        pinned: 'left',
        headerName: ''
    },
    {
        headerName: '公司',
        field: 'outFirm',
        width: 180,
    },
    {
        headerName: '部门',
        field: 'departmentName',
        width: 180,
    },
    {
        headerName: '类型',
        field: 'type',
        width: 180,
    },
    {
        headerName: '固定资产编号',
        field: 'numberId',
        width: 230,
    },
    {
        headerName: '物料大类',
        field: 'majorCategory',
        width: 180,
    },
    {
        headerName: '物料中类',
        field: 'middleCategory',
        width: 150,
    },
    {
        headerName: '物料小类',
        field: 'subCategory',
        width: 180,
    },
    {
        headerName: '物料编号',
        field: 'materialCode',
        width: 180,
    },
    {
        headerName: '物料名称',
        field: 'materialName',
        width: 180,
    },


    {
        headerName: '所属区域',
        field: 'district',
        width: 180,
    },
    {
        headerName: '位置号',
        field: 'floorDistrict',
        width: 180,
    },
    {
        headerName: '分摊年限',
        field: 'shareYears',
        width: 180,
    },
    {
        headerName: '分摊类型',
        field: 'shareType',
        width: 180,
    },
    {
        headerName: '入厂时间',
        field: 'joinFactoryTime',
        width: 180,
    },
    {
        headerName: '备注',
        field: 'remark',
        width: 180,
    },

]
let tableColumns3: any = [
    {
        headerCheckboxSelection: true,
        checkboxSelection: true,
        width: 60,
        showDisabledCheckboxes: false,
        pinned: 'left',
        headerName: ''
    },
    {
        headerName: '物料编号',
        field: 'materialCode',
        width: 200,
    },
    {
        headerName: '物料名称',
        field: 'materialName',
        width: 220,
    },
    {
        headerName: '调出所属区域',
        field: 'district',
        width: 220,
    },

    {
        headerName: '调出位置号',
        field: 'floorDistrict',
        width: 220,
    },
    {
        headerName: '调出部门',
        field: 'departmentName',
        width: 220,
    },
    {
        headerName: '调入所属区域',
        field: 'districtInto',
        width: 220,
        filter: false,
        cellRenderer: {
            template: `
                      <a-select popup-container="html"  class="bacClass" :style="{ width: '180px' }"
                            @click="a" v-model="res.districtInto" allow-clear allow-search>
                            <a-option v-for="item in dataList">{{ item.areaName }}</a-option>
                        </a-select>
                  `,
            setup(props: any) {
                const res = ref(props.params.data)
                const dataList: any = ref()
                const a = () => {
                    if (CompanyId) {
                        post({
                            url: 'planSingle/machineArea/getMachineAreaPara',
                            data: {
                                deleteState: 0,
                                firmId: CompanyId,
                            }
                        }).then(({ code, data }) => {
                            if (code == 200) {
                                dataList.value = data
                            }
                        })
                    } else {
                        Message.error('请选择调入公司')
                    }
                }
                return {
                    res,
                    a,
                    dataList
                }
            }
        }
    },
    {
        headerName: '调入位置号',
        field: 'floorDistrictInto',
        width: 220,
        filter: false,
        cellRenderer: {
            template: `
                      <a-select popup-container="html"  class="bacClass" :style="{ width: '180px' }"
                            @click="a" v-model="res.floorDistrictInto" allow-clear allow-search>
                            <a-option v-for="item in dataList">{{ item.locationMark }}</a-option>
                        </a-select>
                  `,
            setup(props: any) {
                const res = ref(props.params.data)
                const dataList: any = ref()
                const a = () => {
                    post({
                        url: 'planSingle/LocationMarkSet/select',
                        data: {
                            deleteState: 0,
                        }
                    }).then(({ code, data }) => {
                        if (code == 200) {
                            dataList.value = data
                        }
                    })
                }
                return {
                    res,
                    a,
                    dataList
                }
            }
        }
    },


    {
        headerName: '固定资产编号',
        field: 'numberId',
        width: 220,
    },
    {
        headerName: '入厂时间',
        field: 'joinFactoryTime',
        width: 220,
    },
    {
        headerName: '总折旧时长(月)',
        field: 'deprecitionTime',
        width: 220,
    },
    {
        headerName: '调入部门',
        field: 'shiftDepartmentID',
        width: 220,
        filter: false,
        cellRenderer: {
            template: `
               <div>
               <a-cascader v-model="res.shiftDepartmentID" :options="dataList" allow-clear ref="casOb1"
                  style="width: 180px;"  class="bacClass" @click="b" popup-container="html"
                  @change="a" :title="res.title1" />
               </div>
                  `,
            setup(props: any) {
                const res = ref(props.params.data)
                const dataList: any = ref()
                const casOb1: any = ref()

                onMounted(() => {
                    post({
                        url: `org/baCompany/getDepartmentTree?companyId=${CompanyId} `
                    }).then(({ data }) => {
                        dataList.value = data
                    })
                })
                const findAllParentsByValue = (arr: any, targetValue: any, parents = []) => {
                    let result: any[] = [];

                    arr.forEach((item: any) => {
                        if (item.hasOwnProperty('label') && item.value === targetValue) {
                            result = parents.concat(item);
                        } else if (item.hasOwnProperty('children') && Array.isArray(item.children)) {
                            const childResult = findAllParentsByValue(item.children, targetValue, parents.concat(item));
                            if (childResult.length > 0) {
                                result = childResult;
                            }
                        }
                    });

                    return result;
                };

                const a = (val: any) => {
                    res.shiftDepartmentName = ''
                    res.shiftDepartmentID = ''
                    setTimeout(function () {
                        res.value.usePerson = ''
                        if (casOb1.value?.selectViewValue.length != 0) {
                            res.title1 = slicing(casOb1.value?.selectViewValue[0]['label'])
                            // res.value.shiftDepartmentName = casOb1.value?.selectViewValue[0]['label']
                            // res.shiftDepartmentName = casOb1.value?.selectViewValue[0]['label']
                            res.shiftDepartmentID = slicing(casOb1.value?.selectViewValue[0]['value'])
                        }
                    }, 100)
                    const result = findAllParentsByValue(dataList.value, val);

                    if (result.length > 0) {
                        var aaaaa = ""
                        result.map((item: any) => {
                            aaaaa += item.label + '/'
                        })
                        aaaaa = aaaaa.replace(/\/$/, "");
                        res.value.shiftDepartmentName = aaaaa
                        res.shiftDepartmentName = aaaaa
                    }
                }
                const b = async () => {
                    if (CompanyId) {
                        const { code, data
                        } = await post({
                            url: `org/baCompany/getDepartmentTree?companyId=${CompanyId} `
                        })
                        if (code == 200) {
                            dataList.value = data
                        }
                    }
                }
                return {
                    res,
                    a,
                    dataList,
                    b,
                    casOb1,
                    findAllParentsByValue
                }
            }
        }
    },
    {
        headerName: '调入使用人',
        field: 'usePerson',
        width: 150,
        filter: false,
        // cellRenderer: {
        //     template: `
        //               <a-input class="bacClass" v-model="res.usePerson" />
        //           `,
        //     setup(props: any) {
        //         const res = ref(props.params.data)
        //         return {
        //             res
        //         }
        //     }
        // }
        cellRenderer: {
            template: `
            <div @click="viewModal" ><a-input  v-model="res.usePerson" class="bacClass" /></div>
           
            <a-modal v-model:visible="visible" :on-before-ok
            ="handleOk" @cancel="handleCancel" width="800px">
            <template #title>调入使用人</template>
            <a-spin dot :loading="loading" class="w-full h-full">
                <div class="flex justify-between	">
                <el-form @submit.native.prevent>
                <el-form-item label="员工名称" prop="queryName">
                        <el-input v-model="queryName" style="width: 200px" />
                </el-form-item>
                </el-form>
                <el-button type="primary" :icon="Search" @click="getUsers1">查询</el-button>
                </div>
                <div > <AgGridVuesCopys
                    :rowHeight="33"
                    :headerHeight="33"
                    :suppressCopyRowsToClipboard="true"
                    rowSelection="single"
                    @grid-ready="gridReady"
                    style="height: 37vh"
                    :defaultColDef="defaultColDef"
                    class="ag-theme-alpine"
                    :rowData="dataListOP"
                    :columnDefs="formColumns"
                    :localeText="localeText"
                >
                </AgGridVuesCopys>
                
                </div>
                <div class="flex justify-center	"><el-pagination
                small
                background
                layout="prev, pager, next"
                :total="pagination.total"
                v-model:current-page="pagination.page"
                class="mt-4"
                @current-change ="getUsers1"/>
                </div>
            </a-spin>
            </a-modal>
                  `,
            setup(props: any) {

                const dataListOp: any = ref()
                const loading = ref(false)
                const visible = ref(false)
                const res = ref(props.params.data)
                const queryName = ref('')
                const gridOption = ref()
                const pagination = ref({
                    page: 1,
                    size: 10,
                    total: 0
                })
                const gridReady = (params: any) => {
                    gridOption.value = params.api
                }
                const defaultColDef = {
                    sortable: true,
                    resizable: true,
                    filter: true,
                }
                const formData = ref([])
                const formColumns = [
                    {
                        headerCheckboxSelection: false,
                        checkboxSelection: true,
                        width: 60,
                        filter: false,
                        sortable: false,
                        resizable: false,
                        showDisabledCheckboxes: false,
                        pinned: 'left',
                    },
                    {
                        headerName: '员工名称',
                        field: 'name',
                        width: 150,
                    },
                    {
                        headerName: '员工工号',
                        field: 'code',
                        width: 150,
                    },
                    {
                        headerName: '岗位名称',
                        field: 'postName',
                        width: 200,
                    },
                ]
                const getUsers1 = () => {
                    loading.value = true
                    console.log(res, "res");

                    post({
                        url: 'org/hrEmployee/getPage',
                        data: {
                            deleteState: 0,
                            obsoleteState: 0,
                            company_id: CompanyId,
                            ygText: queryName.value,
                            page: pagination.value.page,
                            size: pagination.value.size,
                            department_id: res.value.shiftDepartmentID
                        },
                    }).then(({ data, code }: any) => {
                        if (code === 200) {
                            dataListOp.value = data.list
                            console.log(dataListOp.value)
                            pagination.value.total = data.total
                            loading.value = false
                            gridOption.value.setRowData(data.list)
                        } else {
                            Message.error({
                                content: '该部门无员工信息！',
                                duration: 5000,
                            })
                            loading.value = false
                        }
                    })
                }
                const handleOk = () => {
                    console.log(res);
                    if (gridOption.value.getSelectedRows().length === 0) {
                        ElMessage.warning('请勾选您要填入的员工！')
                        return false
                    } else {
                        res.value.usePerson = gridOption.value.getSelectedRows()[0].name
                        return true
                    }
                    visible.value = false
                }
                const handleCancel = () => {
                    visible.value = false
                }
                const viewModal = () => {
                    if (!res.value.shiftDepartmentID) {
                        ElMessage.warning('请先选择调入部门！')
                        return
                    }
                    queryName.value = ''
                    pagination.total = 0
                    pagination.value.page = 1
                    loading.value = false
                    if (res.value.usePerson) queryName.value = res.value.usePerson

                    getUsers1()
                    visible.value = true
                }
                return {
                    res,
                    getUsers1,
                    visible,
                    dataListOp,
                    loading,
                    handleOk,
                    viewModal,
                    queryName,
                    gridOption,
                    gridReady,
                    formColumns,
                    formData,
                    localeText,
                    defaultColDef,
                    Search,
                    pagination,
                    ElMessage
                }
            }
        }
    },
    {
        headerName: '已折旧时长(月)',
        field: 'upDeprecitionTime',
        width: 220,
    },
]
let tableColumns4: any = [
    {
        headerCheckboxSelection: true,
        checkboxSelection: true,
        width: 60,
        showDisabledCheckboxes: false,
        pinned: 'left',
        headerName: ''
    },
    {
        headerName: '物料编号',
        field: 'materialCode',
        width: 200,
    },
    {
        headerName: '物料名称',
        field: 'materialName',
        width: 220,
    },
    {
        headerName: '调出所属区域',
        field: 'district',
        width: 220,
    },

    {
        headerName: '调出位置号',
        field: 'floorDistrict',
        width: 220,
    },
    {
        headerName: '调出部门',
        field: 'departmentName',
        width: 220,
    },
    {
        headerName: '调入所属区域',
        field: 'districtInto',
        width: 220,
    },
    {
        headerName: '调入位置号',
        field: 'floorDistrictInto',
        width: 220,
    },


    {
        headerName: '固定资产编号',
        field: 'numberId',
        width: 220,
    },
    {
        headerName: '入厂时间',
        field: 'joinFactoryTime',
        width: 220,
    },
    {
        headerName: '总折旧时长(月)',
        field: 'deprecitionTime',
        width: 220,
    },
    {
        headerName: '调入部门',
        field: 'shiftDepartmentName',
        width: 220,
    },
    {
        headerName: '调入使用人',
        field: 'usePerson',
        width: 220,
    },
    {
        headerName: '已折旧时长(月)',
        field: 'upDeprecitionTime',
        width: 220,
    },
]
emitter.on('inCompanyName', (res: any) => {
    CompanyId = res
})

export {
    tableColumns,
    tableColumns1,
    tableColumns2,
    tableColumns3,
    tableColumns4
}